package qz_rpt

class PartInspectionMsgController {

    def commonSqlService
    def dynamicQueryService
    def exportFileService
    def index(Integer max) {
        def cellList = org.kdx.DataUtils.getCellList()
        def pageSizeList = org.kdx.DataUtils.getPageSizeList()

        [totalCount: 5, pageSize: 20, pageCurrent: 1, orderField: 'id', orderDirection: 'asc', pageSizeList: pageSizeList, cellList: cellList]
    }

    def partInspectionMsg(){
       /* SimpleDateFormat sdf = new SimpleDateFormat('yyyy-MM-dd HH:mm:ss')
        def start = params.start ? sdf.parse(params.start) : sdf.parse(sdf.format(new Date().clearTime()))
        def end = params.end ? sdf.parse(params.end) : sdf.parse(sdf.format(new Date()))
        params.start = sdf.format(start)
        params.end = sdf.format(end)*/

        request.setCharacterEncoding("UTF-8")
        response.setContentType("text/html;charset=UTF-8")
        def pageCurrent = params.pageCurrent ? Integer.parseInt(params.pageCurrent) : 1
        def pageSize = params.pageSize ? Integer.parseInt(params.pageSize) : 100
        if (params.export == 'xls') {
            params.startno = 1
            params.endno = 56800
        } else {
            params.startno = (pageSize * (pageCurrent - 1)) + 1
            params.endno = pageSize * pageCurrent
        }

        def pageSizeList = [10,20,50,100]

        def tableHeadList =  []
        def partInspectionMsg = []
        def totalCount = 0
//        def customerLotIdList = commonSqlService.customerLotIdList(params)
        if(params.EXPORT_CONTENT_NAME=='Excel') {
            params.SYS_QUERY_NAME = 'partInspectionMsg'
            partInspectionMsg = dynamicQueryService.queryForList(params)

            if (partInspectionMsg.size() > 0) {
                tableHeadList = partInspectionMsg[0].keySet()
            }
            params.SYS_QUERY_NAME = 'partInspectionMsgCount'
            totalCount = dynamicQueryService.queryForList(params)[0].TOTALCOUNT
        }
//        params.SYS_QUERY_NAME = 'partInspectionMsgCheckInBy'
//        def partInspectionMsgCheckInBy = dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME = 'checkStepList'
        def checkStepList = dynamicQueryService.queryForList(params)
//        params.SYS_QUERY_NAME = 'partInspectionMsgCheckInByName'
//        def partInspectionMsgCheckInByName = dynamicQueryService.queryForList(params)
//        params.SYS_QUERY_NAME = 'partInspectionMsgPartList'
//        def partInspectionMsgPartList = dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME = 'partInspectionMsgLotList'
        def partInspectionMsgLotList = dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME = 'partInspectionMsgFire'
        def partInspectionMsgFire = dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME = 'partInspectionMsgCate'
        def partInspectionMsgCate = dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME = 'partInspectionMsgFinalUser'
        def partInspectionMsgFinalUser = dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME = 'partInspectionMsgGFLB'
        def partInspectionMsgGFLB = dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME = 'partInspectionMsgCheckOutBy'
        def partInspectionMsgCheckOutBy = dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME = 'partInspectionMsgCheckOutByName'
        def partInspectionMsgCheckOutByName = dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME = 'partInspectionMsgIsHege'
        def partInspectionMsgIsHege = dynamicQueryService.queryForList(params)
        print(params.start)
        print(params.end)
        if (params.export == 'xls') {
            def str = g.render(template: "export${params.EXPORT_CONTENT_NAME}", model: [dataList: partInspectionMsg, tableHeadList: tableHeadList])
            exportFileService.exportExcelbyWeb(response, "${params.SYS_QUERY_NAME}", str)
        } else {
            [tableHeadList             : tableHeadList,
             pageSizeList              : pageSizeList, pageSize: pageSize, checkStepList: checkStepList, stepDesc: params.stepDesc ?: '',
//             customerLotIdList: customerLotIdList,
             startno                   : params.startno ?: '', endno: params.endno ?: '', partPicNum: params.partPicNum ?: '', customerLotId: params.customerLotId ?: '',
             partDesc                  : params.partDesc ?: '', step: params.step ?: '', partInspectionMsg: partInspectionMsg, totalCount: totalCount,
//             partInspectionMsgCheckInBy: partInspectionMsgCheckInBy,
//             partInspectionMsgPartList: partInspectionMsgPartList,
             partInspectionMsgLotList  : partInspectionMsgLotList, partInspectionMsgFire: partInspectionMsgFire, partInspectionMsgCate: partInspectionMsgCate,
             partInspectionMsgFinalUser: partInspectionMsgFinalUser, partInspectionMsgGFLB: partInspectionMsgGFLB, partInspectionMsgCheckOutBy: partInspectionMsgCheckOutBy
             , checkinBy               : params.checkinBy ?: '', part: params.part ?: '', lotId: params.lotId ?: '', fire: params.fire ?: '', cate: params.cate ?: '', finalUser: params.finalUser ?: '',
             gflb                      : params.gflb ?: '', checkoutBy: params.checkoutBy ?: '', startTime: params.startTime ?: '', endTime: params.endTime ?: '',
             start                     : params.start ?: '', end: params.end ?: '',
//             partInspectionMsgCheckInByName: partInspectionMsgCheckInByName,
             partInspectionMsgCheckOutByName: partInspectionMsgCheckOutByName,
             checkoutByName            : params.checkoutByName ?: '', checkinByName: params.checkinByName ?: '', partInspectionMsgIsHege: partInspectionMsgIsHege, isHege: params.isHege ?: ''

            ]
        }
    }
    def exportExcel() {
        def sysQueryName


        sysQueryName = params.SYS_QUERY_NAME

        params.startno = 1
        params.endno = 99999999999999999



        params.SYS_QUERY_NAME = sysQueryName

        def dataList = dynamicQueryService.queryForList(params)

        def tableHeadList =  []
        if(dataList.size()>0){
            tableHeadList = dataList[0].keySet()
        }

        print(tableHeadList)

        String filename = new String("${sysQueryName}.xls".getBytes("UTF-8"), "UTF-8");
        response.setContentType('application/vnd.ms-excel;charset=UTF-8')
        response.setHeader('Content-disposition', "attachment;filename=${filename};charset=UTF-8")
        response.setCharacterEncoding("UTF-8");
        render(template: "export${params.EXPORT_CONTENT_NAME}", model: [dataList: dataList,tableHeadList:tableHeadList,startno:params.startno?:'',endno:params.endno?:''])
    }

}
